<template>
  <div class="app-container oneSearchNoFooter">
    <div class="customContainer ">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <dj-store-select :category="579" :defaultStore="formInline.store_id" :needItem="true" @change="selectStoreChanged" />
        <el-form-item>
          <el-date-picker v-model="pickerDate" value-format="yyyy-MM-dd" type="daterange" align="right" unlink-panels range-separator="-"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item> 
        <el-form-item>
          <dj-button size="small" @search="searchClick" />
        </el-form-item>
      </el-form> 
      <!-- 表格 -->
      <dj-simple-table :columns="tableColumns" :data="list" :global="global" :loading="listLoading" />

    </div>
    <!-- 自定义底部 -->
    <div class="bottom_box" v-if="list">
      <span class="pull-right mt10" style="font-size:14px;color:#606266">　出库数量合计：{{returnNum}}&#x3000;&#x3000;&#x3000;　　
        <span style="padding-left:20px">
          出库金额合计：{{returnMoney|keepTwoNum}}
        </span>
        &#x3000;&#x3000;</span>
    </div>
  </div>
</template>
<script>
import { getList } from '@/api/report/booth_out_summary_form';
import { pickerOptionsF, pickerDateF,pickerDateDot, optionMaxTime, getDateString } from '@/utils'; 
import { DjStoreSelect } from '@/components/store';
import { DjSimpleTable } from '@/components/table';
export default {
  components: { 
    'dj-simple-table': DjSimpleTable,
    'dj-store-select': DjStoreSelect
  },
  data() {
    return {
      global: {
        storeName: '', 
        store_id:'',
      },
      tableColumns: [
        {
          label: '序号',
          dtcol: 'index'
        },
        {
          label: '时间',
          dtcol: 'time'
        },
        {
          label: '出库数（个）',
          dtcol: 'out_num',
          link: 'report/booth_out_detail'
        },
        {
          label: '出库金额（元）',
          dtcol: 'out_money',
          isnum: true,
          link: 'report/booth_out_detail'
        }
      ],
      list: [],
      listLoading: false,
      formInline: {
        store_id: this.$store.getters.optionId,
        begin_time: '',
        end_time: '',
        look_del: '0'
      },
      pickerOptions: pickerOptionsF(-1),
      pickerDate: pickerDateDot(-1,-1),
      returnNum: 0,
      returnMoney: 0
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      vm.beforeComingIn();
    });
  },
  methods: {
    beforeComingIn() {
      if (this.$route.query.routerflag) {
        let col = this.$route.query.col;
        // 本月第一天至昨天
        let fist_day = getDateString('this_month_first_day', this.$route.query.global.time);
        let date = new Date(this.$route.query.global.time);
        let now = new Date();
        let yesterday;
        if (date.getMonth() == now.getMonth()) {
          yesterday = getDateString('yesterday');
        } else {
          yesterday = getDateString('this_month_last_day', this.$route.query.global.time);
        }
        // fist_day = fist_day.replace('-', '.').replace('-', '.');
        // yesterday = yesterday.replace('-', '.').replace('-', '.');
        this.pickerDate = [fist_day, yesterday];
        this.formInline.store_id = this.$route.query.global.store_id;
        // this.searchClick();
      }
      //  else {
      //   if (!this.list || this.list.length == 0) {
      //     this.searchClick();
      //   }
      // }
    },
    selectStoreChanged(obj, item) {
      for (let key in obj) this.formInline[key] = obj[key];
      this.global.storeName = item.name;
      this.global.store_id = obj.store_id
      this.searchClick();
    },
    // 搜索
    searchClick() {
      optionMaxTime(this.pickerDate).then(() => {
        //最大可选择时间区间
        this.formInline.begin_time = this.pickerDate ? this.pickerDate[0] : '';
        this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
        this.fetchData();
      });
    },
    // 列表获取
    fetchData() {
      this.listLoading = true; 
      getList(this.formInline)
        .then(response => {
          // console.log(response)
          this.returnNum = 0;
          this.returnMoney = 0;
          this.list = response.data;
          let list = this.list; 
          if (list.length > 0) {
            for (let key in list) {
              list[key].index = parseInt(key) + 1;
              this.returnNum += parseInt(list[key].out_num);
              this.returnMoney += parseFloat(list[key].out_money);
            }
          }
          this.list = list;
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
        });
    }
  }
};
</script>
<style scoped>
.cell a {
  color: #409eff;
}
</style>